package com.lianxi.service.serviceImpl;

import com.lianxi.mapper.ArticleMapper;
import com.lianxi.mapper.CategoryMapper;
import com.lianxi.mapper.LoginMapper;
import com.lianxi.mapper.TagMapper;
import com.lianxi.pojo.Article;
import com.lianxi.pojo.BaseArticle;
import com.lianxi.service.ArticleService;
import com.lianxi.utils.CurrentUtil;
import com.lianxi.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private LoginMapper loginMapper;


    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Map<String, Object> getAllArticle(Integer cid,String title,int current, int size) {
        HashMap<String, Object> hashMap = new HashMap<>();
        int begin = (current - 1) * size;
        Long total = articleMapper.countPubArticle(cid,title);
        List<Article> allArticles = articleMapper.getAllArticle(cid,title,begin, size);
        hashMap.put("total", total);
        hashMap.put("artlists", allArticles);
        return hashMap;

    }

    @Override
    public Map<String, Object> getArtByUid(String title,int state, int current, int size) {
        HashMap<String, Object> hashMap = new HashMap<>();
        int begin = (current - 1) * size;
        String userName = CurrentUtil.getcurrentUserName();
        Integer userId = loginMapper.getUserId(userName);
        Long total = articleMapper.countMyArtByState(title,state, userId);
        List<Article> artlists = articleMapper.getArtByUid(title,userId, state, begin, size);
        hashMap.put("total", total);
        hashMap.put("artlists", artlists);
        return hashMap;
    }

    //发布到草稿箱
    @Override
    public int addArticleToDra(BaseArticle baseArticle) {
        Article article = addAritcle(baseArticle);
        if (baseArticle.getTags() != null && baseArticle.getTags().length > 0) {
            articleMapper.addArticleToDra(article);
            return tagMapper.addTags(article.getId(), baseArticle.getTags());
        } else {
            return articleMapper.addArticleToDra(article);
        }
    }

    @Override
    public Article getOneArt(Integer aid) {
        articleMapper.updaView(aid);
        return articleMapper.getOneArt(aid);
    }

    @Override
    public int updateArt(BaseArticle baseArticle) {
        Integer aid = baseArticle.getId();
        int state = articleMapper.getStateByAid(aid);
        if (state == 1) {
            return upup(baseArticle, 1);
        } else {
            return upup(baseArticle, 2);
        }
    }

    @Override
    public int addArticleToPub(BaseArticle baseArticle) {
        Article article = addAritcle(baseArticle);
        if (baseArticle.getTags() != null && baseArticle.getTags().length > 0) {
            articleMapper.addArticleToPub(article);
            //返回一个结果使用article.getid得到
            return tagMapper.addTags(article.getId(), baseArticle.getTags());
        } else {
            return articleMapper.addArticleToPub(article);
        }
    }


    //草稿箱到发表
    @Override
    public int DraTopub(BaseArticle baseArticle) {
        int[] id = new int[]{baseArticle.getId()};
        int result = tagMapper.deleTag(baseArticle.getId());
        articleMapper.deleArticle(id);
        return addArticleToPub(baseArticle);
    }

    @Override
    public int deleArticle(int[] aids) {
        for (int i = 0; i < aids.length; i++) {
            tagMapper.deleTag(aids[i]);
        }
        return articleMapper.deleArticle(aids);
    }

    @Override
    public int recToRes(Integer aid) {
        return articleMapper.updaState(aid, 2);
    }

    @Override
    public int deleFake(Integer aid) {
        return articleMapper.updaState(aid, 3);
    }

    @Override
    public Map<String, Object> getArtByCid(Integer cid,int current, int size) {
        HashMap<String, Object> hashMap = new HashMap<>();
        int begin = (current - 1) * size;
        Long total = articleMapper.coutArtByCid(cid,1);
        List<Article> allArticles = articleMapper.getArtByCid(cid,1,begin, size);
        System.out.println(allArticles);
        hashMap.put("total", total);
        hashMap.put("artlists", allArticles);
        return hashMap;
    }

    @Override
    public List<Article> getHotArt() {
        return articleMapper.getHotArt();
    }

    public int upup(BaseArticle baseArticle, int state) {
        int result = tagMapper.deleTag(baseArticle.getId());
        Article article = addAritcle(baseArticle);
        article.setState(state);
        if (baseArticle.getTags() != null && baseArticle.getTags().length > 0) {
            tagMapper.addTags(baseArticle.getId(), baseArticle.getTags());
            return articleMapper.updaArticle(article);
        } else {
            return articleMapper.updaArticle(article);
        }
    }

    public Article addAritcle(BaseArticle baseArticle) {
        String userName = CurrentUtil.getcurrentUserName();
        if(baseArticle.getArtImage()=="" || baseArticle.getArtImage()==null){
            baseArticle.setArtImage("http://localhost:8090/images/fd78cc12439c4e039bd1c53ed0cff208.png");
        }
        Integer userId = loginMapper.getUserId(userName);
        Article article = new Article();
        article.setCid(baseArticle.getCid());
        article.setId(baseArticle.getId());
        article.setUid(userId);
        article.setContent(baseArticle.getContent());
        article.setTitle(baseArticle.getTitle());
        article.setCreateTime(DateUtil.getTime());
        article.setEditTime(article.getCreateTime());
        article.setHtmlContent(baseArticle.getHtmlContent());
        article.setArtImage(baseArticle.getArtImage());
        return article;
    }
}
